//
//  VRUserInfoViewController.swift
//  VoiceRoom
//
//  Created by Rui on 2024/3/12.
//

import UIKit

class VRUserInfoViewController: VRBaseViewController {

    private let backButton          = c_make_ex(UIButton.self)
        .image("return").end()
    private let skipButton         = c_make_ex(UIButton.self)
        .text("SKIP").color("#FFFC00").font(16.font_bold).end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(26.font_bold)
        .alignment(.center).text("What is your gender?").end()
    private let subLabel            = c_make_ex(UILabel.self)
        .color(.white.alpha(0.6)).font(16.font_medium)
        .alignment(.center).line(2)
        .text("Choose your gender and let more\npeople know you").end()
    private let maleButton          = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: "#FFFFFF".toColor.alpha(0.16)))
        .bgImage(UIImage.colorToImage(color: "#9C46FF".toColor),state: .selected)
        .image("welcome_male").corner(20).end()
    private let femaleButton        = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: "#FFFFFF".toColor.alpha(0.16)))
        .bgImage(UIImage.colorToImage(color: "#9C46FF".toColor),state: .selected)
        .image("welcome_female").corner(20).end()
    private let maleLabBtn          = c_make_ex(UIButton.self)
        .color(.white,state: .selected).color(.white.alpha(0.4))
        .text("Male").font(16.font_semiBold).end()
    private let femaleLabBtn          = c_make_ex(UIButton.self)
        .color(.white,state: .selected).color(.white.alpha(0.4))
        .text("Male").font(16.font_semiBold).end()
    private let nextButton         = c_make_ex(UIButton.self)
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor))
        .bgImage(UIImage.colorToImage(color: "#FFFC00".toColor.alpha(0.2)),state: .disabled)
        .corner(25).font(18.font_bold).color(.white.alpha(0.2),state: .disabled)
        .text("Next").color(.black).end()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            skipButton
            titleLabel
            subLabel
            maleButton
            femaleButton
            maleLabBtn
            femaleLabBtn
            nextButton
        }
        
        backButton.top(.statusHeight + 2).leading(5).size(40)
        skipButton.trailing(15)
        skipButton.CenterY == backButton.CenterY
        titleLabel.top(.navi_height + 30).centerHorizontally()
        subLabel.centerHorizontally()
        subLabel.Top == titleLabel.Bottom + 10
        maleButton.leading(20).width(160).height(200)
        maleButton.Top == subLabel.Bottom + 45
        femaleButton.trailing(20).width(160).height(200)
        femaleButton.Top == subLabel.Bottom + 45
        maleLabBtn.CenterX == maleButton.CenterX
        maleLabBtn.Top == maleButton.Bottom + 15
        femaleLabBtn.CenterX == femaleButton.CenterX
        femaleLabBtn.Top == femaleButton.Bottom + 15
        nextButton.width(305).height(50).bottom(.safe_bottom + 55).centerHorizontally()
        
        maleButton.isSelected = true
        maleLabBtn.isSelected = true
        femaleLabBtn.isUserInteractionEnabled = false
        maleLabBtn.isUserInteractionEnabled = false
        
        maleButton.addTarget(self, action: #selector(clickMaleButton), for: .touchUpInside)
        femaleButton.addTarget(self, action: #selector(clickFemaleButton), for: .touchUpInside)
        skipButton.addTarget(self, action: #selector(clickSkipButton), for: .touchUpInside)
        nextButton.addTarget(self, action: #selector(clickNextButton), for: .touchUpInside)
        
        UserDefaultsManager.setInt(1, forKey: .currentPage)
    }
    
}


//MARK: action
extension VRUserInfoViewController {
    
    @objc private func clickMaleButton(){
        
        if !maleButton.isSelected {
            
            maleButton.isSelected = true
            maleLabBtn.isSelected = true
            femaleButton.isSelected = false
            femaleLabBtn.isSelected = false
        }
    }
    
    @objc private func clickFemaleButton(){
        
        if !femaleButton.isSelected {
            
            femaleButton.isSelected = true
            femaleLabBtn.isSelected = true
            maleButton.isSelected = false
            maleLabBtn.isSelected = false
        }
    }
    
    @objc private func clickSkipButton(){
        
        ToastTool.show()
        let image = UIImage(named: "welcome_avatar")
        RT.uploadCoverImage(file: (self.imageToBase64String(image: image!))!) { dict in
            let imgUrl = dict.url!
            let dict = ["headImage":imgUrl,"userId":USEINFOMODEL.id ?? "","name":"Voicer"]
            RT.initUserInfoWith(paraInfo: dict as [String : Any]) { info in
                UserDefaultsManager.removeForKey(forKey: .currentPage)                
                UIApplication.window?.rootViewController = VRTabBarViewController()
            }
        }
    }
    
    @objc private func clickNextButton(){
        var gender = 0
        if femaleButton.isSelected {
            gender = 1
        }
        let dict = ["gender":gender,"userId":USEINFOMODEL.id ?? ""] as [String : Any]
        RT.initUserInfoWith(paraInfo: dict as [String : Any]) { info in
            self.navigationController?.pushViewController(VRUserMoreInfoViewController(), animated: true)
        }        
    }
}
